<template>
  <el-card class="page-container" shadow="hover">
    <template #header>
      <div class="header">
        <h3 class="m-0 font-semibold">重置密码</h3>
      </div>
    </template>

    <el-row>
      <el-col :xs="24" :sm="20" :md="16" :lg="12" :xl="10">
        <el-form
          :model="userInfo"
          :rules="rules"
          ref="formRef"
          label-width="100px"
          size="large"
          class="password-form"
        >
          <el-form-item label="新密码" prop="password">
            <el-input
              v-model="userInfo.password"
              type="password"
              placeholder="请输入新密码"
              clearable
              show-password
            />
          </el-form-item>
          <el-form-item label="确认密码" prop="checkpwd">
            <el-input
              v-model="userInfo.checkpwd"
              type="password"
              placeholder="请再次输入密码"
              clearable
              show-password
            />
          </el-form-item>

          <!-- 提交按钮 -->
          <el-form-item>
            <el-button
              type="success"
              size="large"
              @click="updateUserInfo"
              class="bg-[#006400] border-[#006400] w-[100%]"
            >
              提交修改
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <div class="tip">提示：密码长度 3-16 位，不能包含空格</div>
  </el-card>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";

const formRef = ref();

const userInfo = reactive({
  password: "",
  checkpwd: "",
});

// 验证规则
const rules = reactive({
  password: [
    { required: true, message: "请输入新密码", trigger: "blur" },
    { min: 3, max: 16, message: "密码长度为 3 到 16 个字符", trigger: "blur" },
    { pattern: /^\S+$/, message: "密码不能包含空格", trigger: "blur" },
  ],
  checkpwd: [
    { required: true, message: "请确认密码", trigger: "blur" },
    { pattern: /^\S+$/, message: "密码不能包含空格", trigger: "blur" },
    {
      validator: (_rule: any, value: string, callback: any) => {
        if (value !== userInfo.password) {
          callback(new Error("两次输入的密码不一致"));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ],
});

// 提交修改
const updateUserInfo = () => {
  formRef.value?.validate((valid: boolean) => {
    if (!valid) return;
  });
};
</script>

<style lang="scss" scoped>
.page-container {
  border-radius: 10px;
  min-height: 100%;

  .password-form {
    margin-top: 10px;
  }

  :deep(.el-button) {
    background-color: #006400 !important;
    border-color: #006400 !important;
    color: white;
    transition: all 0.3s ease;
  }

  :deep(.el-button:hover) {
    background-color: #004d00 !important;
    border-color: #004d00 !important;
    transform: translateY(-1px);
  }

  .tip {
    margin-left: 100px;
    font-size: 12px;
    color: #999;
    margin-top: 10px;
    padding-bottom: 10px;
  }
}
</style>
